package com.example.jobback.service.manager.impl;

import com.example.jobback.config.AuthConfig;
import com.example.jobback.mapper.ManagerMapper;
import com.example.jobback.po.Manager;
import com.example.jobback.po.User;
import com.example.jobback.req.common.password.ResetPasswordReq;
import com.example.jobback.req.manager.profile.ManagerReq;
import com.example.jobback.resp.manager.profile.ManagerResp;
import com.example.jobback.result.Result;
import com.example.jobback.service.common.UserCommonService;
import com.example.jobback.service.manager.ManagerProfileService;
import com.example.jobback.util.PhoneNumberUtils;
import com.example.jobback.util.SecureEncryptionUtil;
import com.example.jobback.util.TLUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.concurrent.TimeUnit;

import static com.example.jobback.util.RedisConstants.MANAGER_KEY;
import static com.example.jobback.util.RedisConstants.MANAGER_TTL;

/**
 * @ClassName: ManagerProfileServiceImpl
 * @Description:管理员-个人中心服务实现类
 * @Author: lhb
 * @Date: 2025/6/21
 */
@Service
public class ManagerProfileServiceImpl implements ManagerProfileService {

    private final AuthConfig authConfig;

    private final ManagerMapper managerMapper;

    private final UserCommonService userCommonService;

    public ManagerProfileServiceImpl(AuthConfig authConfig, ManagerMapper managerMapper, UserCommonService userCommonService) {
        this.authConfig = authConfig;
        this.managerMapper = managerMapper;
        this.userCommonService = userCommonService;
    }

    /**
     * @Description: 获取基本信息
     * @return Result<ManagerResp>
     * @Author lhb
     * @CreateTime 2025/6/21 15:16
     */
    @Override
    public Result<ManagerResp> getBasicMessage() {
        Manager manager= TLUtil.get(TLUtil.M);
        String phone = null;
        try {
            phone = SecureEncryptionUtil.decrypt(manager.getPhoneAes(), authConfig.getPhoneSecret());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        ManagerResp managerResp=new ManagerResp();
        Long age=0L;
        LocalDateTime birthDate=manager.getBirthDate();
        if (birthDate!=null) {
            age = (long) Period.between(LocalDate.from(birthDate), LocalDate.now()).getYears();
        }
        managerResp.setAge(age);
        //带*处理
        String maskedPhone = PhoneNumberUtils.maskPhoneNumber(phone);
        managerResp.setPhone(maskedPhone);
        BeanUtils.copyProperties(manager, managerResp);
        return Result.success(managerResp);
    }

    /**
     * @Description: 编辑基本信息
     * @param req
     * @return Result<Object>
     * @Author lhb
     * @CreateTime 2025/6/21 15:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> editBasicMessage(ManagerReq req) {
        Manager manager=TLUtil.get(TLUtil.M);
        BeanUtils.copyProperties(req, manager);
        //修改user的各角色的共同信息
        User user=new User();
        BeanUtils.copyProperties(manager, user);
        if(userCommonService.editBasicMessage(user)==0) throw new RuntimeException("修改失败");
        //修改管理员的个别信息，目前没有什么信息
        if(managerMapper.editBasicMessage(manager)==0) throw new RuntimeException("修改失败");
        String token = userCommonService.setRedisUser(manager,manager.getUserId(),MANAGER_KEY,authConfig.getJwtManagerSecret(),MANAGER_TTL, TimeUnit.DAYS);
        if(token==null) throw new RuntimeException("token生成失败");
        return Result.success(token);
    }

    /**
     * @Description: 上传头像
     * @param file
     * @return Result<Object>
     * @Author lhb
     * @CreateTime 2025/6/21 15:20
     */
    @Override
    public Result<Object> uploadAvatar(MultipartFile file) {
        Manager manager=TLUtil.get(TLUtil.M);
        //user对象的userid
        Long userId=manager.getUserId();
        String catalog="Avatar/User";
        String lastUrl = userCommonService.uploadAvatar(userId,file,catalog);
        if(lastUrl==null) throw new RuntimeException("上传失败");
        if(userCommonService.updateUserAvatar(userId,lastUrl)==0) throw new RuntimeException("上传失败");
        manager.setAvatar(lastUrl);
        String token = userCommonService.setRedisUser(manager,manager.getUserId(),MANAGER_KEY,authConfig.getJwtManagerSecret(),MANAGER_TTL,TimeUnit.DAYS);
        return Result.success(token);
    }
}
